package com.baosight.risk.service.fltd.impl;

import com.baosight.risk.api.fltd.AuthoTypeService;
import com.baosight.risk.api.ridc.RidcRiskService;
import com.baosight.risk.api.xtzy.IXtzyCompanyService;
import com.baosight.risk.api.xtzy.IXtzyRoleUserService;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.fltd.FltdTaskMapper;
import com.baosight.risk.mapper.riim.RiimRiskImportMapper;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoTypeVO;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.riim.entity.RiimRiskImport;
import com.baosight.risk.service.system.entity.SysRole;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 重点风险权限 importRiskRole
 */
@Slf4j
@Service("importRiskRole")
public class ImportRiskRoleAuthoTemplate implements AuthoTypeService {

    @Autowired
    private RidcRiskService ridcRiskService;

    @Autowired
    private IXtzyCompanyService companyService;

    @Autowired
    private IXtzyRoleUserService roleUserService;

    @Autowired
    private AuthoTypeTemplate authoTypeService;

    @Autowired
    private RiimRiskImportMapper riimRiskImportMapper;

    @Autowired
    private FltdTaskMapper fltdTaskMapper;

    @Override
    public Map<String, Object> parse(String template, Map<String, Object> param) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        FlaaNodeAuthoTypeVO authInfo = (FlaaNodeAuthoTypeVO) param.get("authInfo");
        FltdTaskVO currentTask = (FltdTaskVO) param.get("currentTask");
        //流程参与者
        List<XtzyUser> userList = (List<XtzyUser>) param.get("userList");
        try {
            String bizType = "";
            if (AuthoTypeService.RIIM_1.equals(authInfo.getRoleCode())) {
                //推进人直属领导（部门领导/部门分管领导）
                RiimRiskImport riimImport = riimRiskImportMapper.selectByPrimaryKey(currentTask.getTaskGuid());
                if (riimImport == null){
                    FltdTask fltdTask = new FltdTask();
                    fltdTask.setTodoGuid(currentTask.getTodoGuid());
                    List<FltdTask> select = fltdTaskMapper.select(fltdTask);
                    if (!select.isEmpty()){
                        FltdTask fltdTask1 = select.get(0);
                        String taskGuid = fltdTask1.getTaskGuid();
                        riimImport = riimRiskImportMapper.selectByPrimaryKey(taskGuid);
                    }
                }
                //RiimRiskImport riimImport = riimRiskImportMapper.selectByPrimaryKey("f495ad84-7bc8-4174-a7a1-12def5804dd3");
                String deptCode = riimImport.getExt8();
                String deptName = riimImport.getExt9();
                //分管领导
                List<XtzyUser> newUsers = companyService.getUserAndCompIdAndRoleId(riimImport.getExt8(), "4430017a-4446-4b1f-8996-cdfa1a030284");
                for (XtzyUser user : newUsers) {
                    user.setExt1(deptCode);
                    user.setExt2(deptName);
                }
                //直属领导
                List<XtzyUser> newUsers2 = companyService.getUserAndCompIdAndRoleId(riimImport.getExt8(), "56c21cb0-2b7e-4947-9a0e-dde4d6189ae6");
                for (XtzyUser user : newUsers2) {
                    user.setExt1(deptCode);
                    user.setExt2(deptName);
                }
                newUsers.addAll(newUsers2);
                authoTypeService.unionUsers(userList, newUsers);
            } else if (AuthoTypeService.RIIM_3.equals(authInfo.getRoleCode())) {
                //项目推进人
                bizType = "RISK_IMPORT.DUTY_USER";
                List<XtzyUser> newUsers = new ArrayList<>();
                RiimRiskImport riimImport = riimRiskImportMapper.selectByPrimaryKey(currentTask.getTaskGuid());
                String dutyUserCode = riimImport.getDutyUserCode();
                if (StringUtils.isNotEmpty(dutyUserCode)) {
                    //查询本单位及其下属部门
                    List<XtzyCompany> companyList = companyService.selectAllSubsetByParentID(riimImport.getDeptCode());
                    List<String> compCodes = new ArrayList<>();
                    for (XtzyCompany xtzyCompany : companyList) {
                        compCodes.add(xtzyCompany.getCompCode());
                    }
                    List<SysRole> userRoles = roleUserService.getRolesByUserCode(compCodes, riimImport.getDutyUserCode());
                    boolean flag = false;
                    for (SysRole userRole : userRoles) {
                        //必须是项目推进人角色
                        if ("TASK01".equals(userRole.getRoleKey())) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag) {
                        XtzyUser user = new XtzyUser();
                        user.setUserCode(riimImport.getDutyUserCode());
                        user.setUserDesc(riimImport.getDutyUserName());
                        user.setExt1(riimImport.getDeptCode());
                        user.setExt2(riimImport.getDeptName());
                        newUsers.add(user);
                        authoTypeService.unionUsers(userList, newUsers);
                    }
                }
            } else if (AuthoTypeService.RIIM_4.equals(authInfo.getRoleCode())) {
                //评审小组
                bizType = "RISK_FINISH.GROUP_USER";
                Map<String, String> map = new HashMap<>();
                map.put("bizGuid", currentTask.getBizGuid());
                map.put("taskGuid", currentTask.getTaskGuid());
                map.put("bizType", bizType);
                List<XtzyUser> newUsers = ridcRiskService.getUser(map);
                authoTypeService.unionUsers(userList, newUsers);
            }
        } catch (Exception e) {
            throw new Exception("根据重点风险授权关系查找审批人异常！");
        }
        return resultMap;
    }

}
